package com.ruoyi.binance.task;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.binance.api.OrderApi;
import com.ruoyi.binance.dingding.DingDingSendMsgUtils;
import com.ruoyi.binance.domain.BinanceTransactionConf;
import com.ruoyi.binance.domain.BinanceTransactionOrder;
import com.ruoyi.binance.mapper.BinanceTransactionConfMapper;
import com.ruoyi.binance.mapper.BinanceTransactionOrderMapper;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Component
public class OrderTask {
    private static final Logger log = LoggerFactory.getLogger(OrderTask.class);

    @Autowired
    private BinanceTransactionOrderMapper binanceTransactionOrderMapper;
    @Autowired
    private BinanceTransactionConfMapper binanceTransactionConfMapper;
    @Value("${stock.dingdingMessage}")
    private boolean dingdingMessage;

    @Scheduled(cron = "0/3 * * * * ?")
    public void query() {
        try {
            log.info("订单状态同步定时任务----------------------开始");
            List<BinanceTransactionOrder> binanceTransactionOrders = binanceTransactionOrderMapper.queryOrderList();
            for (BinanceTransactionOrder bean : binanceTransactionOrders) {
                JSONObject jsonObject = OrderApi.queryOrder(bean.getCode(), bean.getOrgOrderId());
                if("-2013".equals(jsonObject.getString("code"))){
                    bean.setStatus("REJECTED");
                    bean.setRemark(jsonObject.getString("msg"));
                    binanceTransactionOrderMapper.updateBinanceTransactionOrder(bean);
                }
                //更新订单状态
                bean.setPrice(jsonObject.getBigDecimal("price"));
                if("BUY".equals(bean.getSide())) {
                    bean.setExecutedQty(jsonObject.getBigDecimal("executedQty"));
                }
                if("SELL".equals(bean.getSide())){
                    bean.setExecutedQty(jsonObject.getBigDecimal("cummulativeQuoteQty"));
                }
                bean.setTransactTime(jsonObject.getDate("time"));
                bean.setStatus(jsonObject.getString("status"));
                binanceTransactionOrderMapper.updateBinanceTransactionOrder(bean);

                if ("FILLED".equals(bean.getStatus())) {
                    //更新交易配置
                    BinanceTransactionConf binanceTransactionConf = binanceTransactionConfMapper.selectBinanceTransactionConfById(bean.getConfigId());
                    if (binanceTransactionConf != null){
                        if("BUY".equals(bean.getSide())){
                            binanceTransactionConf.setSide("BUY");
                            binanceTransactionConf.setCoinNumber(bean.getExecutedQty());
                            binanceTransactionConf.setUsdtNumber(new BigDecimal("0"));
                        }
                        if("SELL".equals(bean.getSide())){
                            binanceTransactionConf.setSide("SELL");
                            binanceTransactionConf.setUsdtNumber(jsonObject.getBigDecimal("cummulativeQuoteQty"));
                            binanceTransactionConf.setCoinNumber(new BigDecimal("0"));
                        }
                        binanceTransactionConfMapper.updateBinanceTransactionConf(binanceTransactionConf);
                    }
                    if(dingdingMessage){
                        //交易成功进行钉钉通知
                        String tranMessageTemplate = DingDingSendMsgUtils.getTranMessageTemplate(bean);
                        DingDingSendMsgUtils.sendMessageAtAll(tranMessageTemplate);
                    }
                }
            }
            log.info("订单状态同步定时任务----------------------结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
